<?php
defined('IN_IA') or exit('Access Denied');
global $_W, $_GPC;
load()->func('file');
if(mkdirs(ATTACHMENT_ROOT . '/bsht_tbk/' . $_W['uniacid'])) true;
set_time_limit(60);
include_once '../addons/bsht_tbk/plus/excel/oleread.php';
include_once '../addons/bsht_tbk/plus/excel/excel.php';
$error = $_FILES["txtfile1"]["error"];
if (!empty($_FILES['txtfile1']['name']) && $error == UPLOAD_ERR_OK) {
	$tmp_name = $_FILES["txtfile1"]["tmp_name"];
	$name = $_FILES["txtfile1"]["name"];
	$filename = ATTACHMENT_ROOT . '/bsht_tbk/' . $_W['uniacid'] . '/gy-bsht-tbk-' . date('Ymdhis') . '.' . pathinfo($name, PATHINFO_EXTENSION);

	if (move_uploaded_file($tmp_name, $filename)) {
		$xls = new Spreadsheet_Excel_Reader();
		$xls->setOutputEncoding('utf-8');
		$xls->read($filename);
		for ($i = 2; $i <= $xls->sheets[0]['numRows']; $i++) {
			$data_values[] = $xls->sheets[0]['cells'][$i];
		}
	}


	$inert_sql = "INSERT INTO " . tablename('bsht_tbk_caiji') . " (*) ";
	$insert_val = "";
	$count = 0;
	$cateid = 0;
	foreach ($data_values as $value) {
		if ($value[1] == '') {
			break;
		}
		if ($value[1] != '') {
			$cate = pdo_fetch('select id,itemid from ' . tablename('bsht_tbk_caiji') . " where itemid = '{$value[1]}' and uniacid = '{$_W['uniacid']}' limit 1");
			if (empty($cate)) {
				$cate_data = array('uniacid' => $_W['uniacid'], 'displayorder' => '0', 'itemid' => $value[1], 'title' => $value[2], 'itempic' => $value[3], 'shoptitle' => $value[5], 'itemfee' => $value[6], 'itemmsell' => $value[7], 'itemstatus' => $value[10], 'stime' => $value[13], 'etime' => $value[14], 'itemsurl' => $value[16], 'itemurl' => $value[17], 'itemtkl' => $value[18], 'itemyhj_zl' => $value[19], 'itemyhj_yl' => $value[20], 'itemyhj_tit' => $value[21], 'itemyhj_stime' => $value[22], 'itemyhj_etime' => $value[23], 'itemyhj_url' => $value[24], 'itemyhj_tkl' => $value[25], 'status' => 1);
				pdo_insert('bsht_tbk_caiji', $cate_data);
				$cateid = pdo_insertid();
			} else {
				$cateid = $cate['id'];
			}
			continue;
		}
		if (!empty($insert_val)) {
			$insert_val .= ", ";
		}
		$insert_val .= " ( * ) ";
		$count++;
	}
	message('导入成功！', referer(), 'success');
} else {
	//message('导入失败，请检查文件或运行环境！', referer(), 'error');
}